package com.kpx.app.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;

import com.kpx.app.R;
import com.kpx.app.activity.ActivityMyPhotosNew;
import com.kpx.app.activity.ActivityOrderDetail;
import com.kpx.app.activity.ActivityOrderUploadPics;
import com.kpx.app.activity.ActivityUserInfo;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.base.ComAdapter;
import com.kpx.app.base.ComViewHolder;
import com.kpx.app.bean.OrderBean;
import com.kpx.app.bean.OrderDetailBean;
import com.kpx.app.bean.OrderModelPhoto;
import com.kpx.app.bean.PayInfoBean;
import com.kpx.app.http.HttpDataApi;
import com.kpx.app.http.HttpDataLoader;
import com.kpx.app.http.HttpDataReqUrl;
import com.kpx.app.http.ResObj;
import com.kpx.app.utils.ImageLoader;
import com.kpx.app.utils.MyHashMap;
import com.kpx.app.utils.UIUtils;
import com.kpx.app.utils.UserInfoUtils;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.wigdet.DialogFactoryImpl;

import java.util.ArrayList;
import java.util.List;

import cn.finalteam.galleryfinal.PhotoPreviewActivity;
import cn.finalteam.galleryfinal.model.PhotoInfo;

/**
 * Created by congdy on 17/4/16.
 */

public class PicAdapter extends ComAdapter {

    private ArrayList<PhotoInfo> piclist;
    private OrderBean order;

    /**
     * 构造方法
     *
     * @param context  上下文对象
     * @param datas    数据
     * @param layoutId
     * @version 1.0
     * @createTime 2015年5月24日, 上午11:47:30
     * @updateTime 2015年5月24日, 上午11:47:30
     * @createAuthor chencong
     * @updateAuthor chencong
     * @updateInfo (此处输入修改内容, 若无修改可不写.)
     */
    public PicAdapter(Context context, List datas, int layoutId) {
        super(context, datas, layoutId);
    }

    @Override
    public void convert(ComViewHolder holder, final Object o, final int position) {
        if (o instanceof OrderDetailBean.PhotosListBean) {
            holder.setImageResource(R.id.img_pic, R.drawable.progress);
            holder.setVisibility(R.id.img_delete, (order != null && (order.getOrderStatus() == OrderBean.ORDER_STATUS_WAITTING_UPLOAD
                    || order.getOrderStatus() == OrderBean.ORDER_STATUS_REFUESD_BY_PINTAI
                    || order.getOrderStatus() == OrderBean.ORDER_STATUS_REFUESD_BY_SELLER)) ? ComViewHolder.VISIBLE : ComViewHolder.GONE)
                    .setImageURI(R.id.img_pic, ((OrderDetailBean.PhotosListBean) o).getImgs())
                    .setOnClickListener(R.id.img_delete, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            DialogFactoryImpl.newInstance(mContext).showConfirmDialog("取消", "确定", "确定删除该图片吗", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                    switch (which) {
                                        case DialogInterface.BUTTON_NEGATIVE:
                                            break;
                                        case DialogInterface.BUTTON_POSITIVE:
                                            toDeletePic((OrderDetailBean.PhotosListBean) o);
                                            break;
                                    }
                                }
                            });

                        }
                    });
            ;
            holder.getConvertVeiw().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (piclist == null) {
                        piclist = new ArrayList<PhotoInfo>();
                        for (int i = 0; i < mDatas.size(); i++) {
                            PhotoInfo p = new PhotoInfo();
                            p.setPhotoPath(((OrderDetailBean.PhotosListBean) mDatas.get(i)).getImgs());
                            Bitmap bitmap = ImageLoader.load(mContext, p.getPhotoPath());
                            if (bitmap != null) {
                                p.setWidth(bitmap.getWidth());
                                p.setHeight(bitmap.getHeight());
                            }
                            piclist.add(p);
                        }
                    }
                    UIUtils.goShowPic(mContext,piclist,position);
                }
            });
        } else if (o instanceof PhotoInfo) {
            final PhotoInfo b = (PhotoInfo) o;
            holder.setVisibility(R.id.img_delete, position == mDatas.size() - 1 ? ComViewHolder.GONE : ComViewHolder.VISIBLE);
            holder.setImageResource(R.id.img_pic, R.drawable.add_pic);
            if (position == mDatas.size() - 1) {
                holder.setImageResource(R.id.img_pic, R.drawable.add_pic).setOnClickListener(R.id.img_pic, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ((ActivityOrderUploadPics) mContext).selectPhoto();
                    }
                });
            } else {
                holder.setImageLocal(R.id.img_pic, b.getPhotoPath()).setOnClickListener(R.id.img_delete, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mDatas.remove(b);
                        notifyDataSetChanged();
                    }
                });
            }
        }else if(o instanceof OrderModelPhoto){
            final OrderModelPhoto b = (OrderModelPhoto) o;
            holder.setVisibility(R.id.img_delete,ComViewHolder.GONE).setImageURI(R.id.img_pic, b.getImgs()).setOnClickListener(R.id.img_pic, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (piclist == null) {
                        piclist = new ArrayList<PhotoInfo>();
                        for (int i = 0; i < mDatas.size(); i++) {
                            PhotoInfo p = new PhotoInfo();
                            p.setPhotoPath(((OrderModelPhoto) mDatas.get(i)).getImgs());
                            Bitmap bitmap = ImageLoader.load(mContext, p.getPhotoPath());
                            if (bitmap != null) {
                                p.setWidth(bitmap.getWidth());
                                p.setHeight(bitmap.getHeight());
                            }
                            piclist.add(p);
                        }
                    }
                    UIUtils.goShowPic(mContext,piclist,position);
                }
            });;
        }
    }

    private void toDeletePic(final OrderDetailBean.PhotosListBean o) {
        ((ActivityBase) mContext).showLoadingDialog();
        final MyHashMap map = new MyHashMap();
        map.put("photoId", o.getDmId() + "");
        if(order!=null)
        map.put("orderId", order.getDmId() + "");
        new HttpDataLoader(new HttpDataApi.OnRequestCallback() {
            @Override
            public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
                ((ActivityBase) mContext).dissmissLoadingDialog();
                if (result.getCode() == 0) {
                    mDatas.remove(o);
                    notifyDataSetChanged();
                    ToastUtil.getInstance().showToast(mContext, "删除成功");
                    if(mContext instanceof ActivityOrderUploadPics){
                        ((ActivityOrderUploadPics)mContext).reFreshUpCount(mDatas.size());
                    }else
                    if(mContext instanceof ActivityOrderDetail){
                        ((ActivityOrderDetail)mContext).reFreshUpCount(mDatas.size());
                    }
                } else {
                    ToastUtil.getInstance().showToast(mContext, result.getMsg());
                }
            }

            @Override
            public void onRequestError(String url, int statusCode, String error) {
                ((ActivityBase) mContext).dissmissLoadingDialog();
                ToastUtil.getInstance().showToast(mContext, error);
            }

        }, null, null).POST(HttpDataReqUrl.URL_POST_GET_DELETE_ORDER_PIC, true, map);
    }

    public void setOrderBean(OrderBean b) {
        this.order = b;
    }
}
